<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>

      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入类目名称" clearable>
            <el-button slot="append" icon="el-icon-search" v-model.trim="query.typeName"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="openAdd">添加商品类目</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodstypeList" border style="width: 50%" :tree-props="treeProps"
                row-key="id">
        <el-table-column prop="typeName" label="类目名称" width="180"></el-table-column>
        <el-table-column label="是否有效" width="180">
          <template v-slot:="scope">
            <i class="el-icon-success" v-if="scope.row.state ==='0' " style="color: #85ce13"></i>
            <i class="el-icon-error" v-else style="color: red"></i>
          </template>
        </el-table-column>

        <el-table-column label="类级" width="180">
          <template v-slot:="scope">
            <el-tag v-if="scope.row.typeSort === '1'">一级</el-tag>
            <el-tag type="success" v-else-if="scope.row.typeSort === '2'">二级</el-tag>
            <el-tag type="warning" v-else>三级</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="250px">
          <template v-slot:="scope">
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="query.current"
                     :page-size="query.size" :total="total" :page-sizes="[1, 2, 3, 50]"
                     layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>

    </el-card>

    <!--      添加商品类目-->
    <el-dialog :close-on-click-modal="false"
               title="添加商品类目" @close="closeAddDialog"
               :visible.sync="openAddDialog" width="30%">
      <el-form :model="goodsTypeForm" :rules="goodsTypeFormRule" ref="goodsTypeFormRef"
               status-icon  label-width="100px">

        <el-form-item label="类目名称" prop="typeName">
          <el-input v-model.trim="goodsTypeForm.typeName"></el-input>
        </el-form-item>

        <el-form-item label="归属">
          <el-cascader style="width: 100%;" size="medium" clearable
                       ref="cascaderRef" :options="selectOptions"
                       :props="cascaderProps" @change="handleChange">
          </el-cascader>
        </el-form-item>

      </el-form>

      <span slot="footer">
        <el-button type="primary" @click="add">确 定</el-button>
        <el-button @click="openAddDialog = false">取 消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'GoodsType',
  data () {
    return {
      query: {
        typeName: '',
        current: 1,
        size: 2
      },
      total: 0,
      goodstypeList: [],
      treeProps: {
        children: 'items',
        hasChildren: 'hasChildren'
      },
      openAddDialog: false,
      goodsTypeForm: {
        typeName: '',
        typeSort: 1,
        pid: 0
      },
      goodsTypeFormRule: {
        typeName: [{
          required: true,
          message: '类目名称未填写',
          trigger: 'blur'
        }]
      },
      selectOptions: [],
      cascaderProps: {
        expandTrigger: 'hover',
        children: 'items',
        label: 'typeName',
        value: 'id',
        checkStrictly: true,
        // leaf: 'typeSort'
      }
    }
  },
  created () {
    this.getGoodstypeList()
  },
  mounted () {
    // 级联选择器去掉radio后选择文字取值
    setInterval(function () {
      document.querySelectorAll('.el-cascader-node__label').forEach(el => {
        el.onclick = function () {
          if (this.previousElementSibling) this.previousElementSibling.click()
        }
      })
    }, 1000)
  },
  methods: {
    async getGoodstypeList () {
      const { data: res } = await this.$http.get('goodstype/getTypeList', { params: this.query })
      this.goodstypeList = res.result.records
      this.total = res.result.total
    },
    async getSelectGoodstypeList () {
      const { data: res } = await this.$http.get('goodstype/getSelectList')
      this.selectOptions = res.result
    },
    handleSizeChange (val) {
      this.query.size = val
      this.getGoodstypeList()
    },
    handleCurrentChange (val) {
      this.query.current = val
      this.getGoodstypeList()
    },
    openAdd () {
      this.openAddDialog = !this.openAddDialog
      this.getSelectGoodstypeList()
    },
    closeAddDialog () {
      this.$refs.goodsTypeFormRef.resetFields()
      this.$refs.cascaderRef.$refs.panel.clearCheckedNodes()
      this.goodsTypeForm = {
        typeName: '',
        typeSort: 1,
        pid: 0
      }
    },
    add () {
      this.$refs.goodsTypeFormRef.validate(async valit => {
        if (valit) {
          const { data: res } = await this.$http.post('goodstype/addGoodsType', this.$Qs.stringify(this.goodsTypeForm))
          if (!res.success) {
            return this.$message.error(res.message)
          }
          this.$message.success(res.message)
          this.openAddDialog = !this.openAddDialog
          this.getGoodstypeList()
        }
      })

    },
    handleChange (val) {
      console.log(val)
      if (this.$refs.cascaderRef.getCheckedNodes().length > 0) {
        this.goodsTypeForm.typeSort = this.$refs.cascaderRef.getCheckedNodes()[0].level + 1
        this.goodsTypeForm.pid = this.$refs.cascaderRef.getCheckedNodes()[0].value
      }

    }
  }
}
</script>

<style scoped lang="less">

</style>
